﻿using ZYCommon;
using System;
using System.IO;
using System.Windows.Forms;
using System.Xml;
using Bifrost;

namespace TextEditor
{
    

    public class A_SaveAs : A_Action
    {   
        public override string ActionName()
        {
            return "saveas";
        }

        public override A_Action Clone()
        {
            A_SaveAs as2 = new A_SaveAs();
            as2.BaseCloneFrom(this);
            return as2;
        }

        public override bool Execute()
        {
            base.myOwnerDocument.ToXMLFile(base.Param1);
            base.myOwnerDocument.Modified = false;
            return true;
        }

        private void SaveAs(object sender, EventArgs e)
        {
            UIExecute();
        }

        public override bool UIExecute()
        {
            using (SaveFileDialog dialog = new SaveFileDialog())
            {
                dialog.FileName = base.myOwnerDocument.Info.FileName;
                dialog.CheckFileExists = false;
                dialog.CheckPathExists = true;
                dialog.OverwritePrompt = true;
                dialog.Title = "将文档另存为";
                dialog.Filter = "XML文件或Base64编码文件|*.xml;*.txt|所有文件|*.*";
                if (dialog.ShowDialog() == DialogResult.OK)
                {
                    base.myOwnerDocument.ToXMLFile(dialog.FileName);
                    base.myOwnerDocument.Modified = false;
                    //base.myOwnerDocument.Info.SaveMode = 0;
                    //if (dialog.FileName.ToUpper().EndsWith("XML"))
                    //{
                    //    base.myOwnerDocument.ToXMLFile(dialog.FileName);
                    //}
                    //else
                    //{
                    //    XmlDocument myDoc = new XmlDocument();
                    //    base.myOwnerDocument.ToXMLDocument(myDoc);
                    //    string str = StringCommon.FormatBase64String(StringCommon.ToBase64String(myDoc.DocumentElement.OuterXml), 8, 8);
                    //    StreamWriter writer = new StreamWriter(dialog.FileName);
                    //    writer.Write(str);
                    //    writer.Close();
                    //}
                    //base.myOwnerDocument.Modified = false;
                }                              
            }
            return true;
        }
    }
}

